<html>
<?php
    if(isset($_POST['selDBMS']))
        $dbms = $_POST['selDBMS'];
    $pathAdjust = "../";
    @include "{$pathAdjust}initIncludePaths.inc";
    $gotoPage = "finish.php";
    @include "installCheck.inc";
    /*
    * This script establishes the first administrator for the
    * instance of 2DBv2.
    */
    @include('display.inc');
    $disp = new Display();
?>
     <head>
        <title>2DBv2 Setup - Administrator Information</title>
        <?php   @include "metaTags.inc";
                @include "head.inc";  ?>
    </head>
    <body>
<?php
    $db = null;
    $dbfac = new DatabaseFactory();
    $conDataOK = $dbfac->testSettings();
    if(!$conDataOK)
        echo "
            <script type=\"text/javascript\" language=\"JavaScript\">
                this.location.href=\"setupServer.php\";
            </script>
        ";
    else
        $db = $dbfac->connect();
    @include('user.inc');
    $user = new User($db);
    if(isset($_POST['submit'])) {
        //data has been entered into the form and needs to be checked and stored now.
        $fName = $_POST['fName'];
        $lName = $_POST['lName'];
        $initials = $fName[0] . $lName[0];
        if($_POST['pass1'] == $_POST['pass2']) {
            $pass = sha1($pass1);
            $user = new User($db,$fName,$_POST['mName'],$lName,$initials,$_POST['email'],$pass,"OK",$_POST['bio'],$_POST['wPage']);
            $user->create("administrator");
        }
        if($db->hasAdmins())
            $disp->frameText("good", "Step 3 of 4 successfully completed. <a href=\"finish.php\">Next step</a>.");
        else
            $disp->frameText("bad", "There is a problem with your information an admin could not be created.");
    } else {
        //check whether an admin exists and if so send to the next step.
        if($db->hasAdmins())
            echo "
                <script type=\"text/javascript\" language=\"JavaScript\">
                    this.location.href=\"finish.php\";
                </script>
            ";
    }
    $dbmss = $dbfac->determineAvailableDBMS();
?>

        <div>
            Some info needed here.
        </div>
        <br />
        <form action="setupAdmin.php" method="post">
            <table>
                <tr class="required">
                    <td>First Name</td>
                    <td>
                        <input type="text" name="fName" value="<?php echo $admin->getFirstName(); ?>" />
                    </td>
                </tr>
                <tr>
                    <td>Middle Names</td>
                    <td>
                        <input type="text" name="mNames" value="<?php echo $admin->getMiddleNames(); ?>" />
                    </td>
                </tr>
                <tr class="required">
                    <td>Last Name</td>
                    <td>
                        <input type="text" name="lName" value="<?php echo $admin->getLastName(); ?>" />
                    </td>
                </tr>
                <tr class="required">
                    <td>Email</td>
                    <td>
                        <input type="text" name="email" value="<?php echo $admin->getEmail(); ?>" />
                    </td>
                </tr>
                <tr class="required">
                    <td>Password</td>
                    <td>
                        <input type="password" name="pass1" value="<?php echo $admin->getPassword(); ?>" />
                    </td>
                </tr>
                <tr class="required">
                    <td>Repeat Password</td>
                    <td>
                        <input type="password" name="pass2" value="<?php echo $admin->getPassword(); ?>" />
                    </td>
                </tr>
                <tr>
                    <td>Web Page</td>
                    <td>
                        <input type="text" name="wPage" value="<?php echo $admin->getWebPage(); ?>" />
                    </td>
                </tr>
                <tr>
                    <td>Short Bio</td>
                    <td>
                        <textarea cols="20" rows="5" name="bio">
                            <?php echo $admin->getInfo(); ?>
                        </textarea>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" name="submit" value="Submit" />
                        <input type="submit" name="cancel" value="Cancel" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>